package com.handtransaction.controller;

import com.handtransaction.entity.Goods;
import com.handtransaction.entity.User;
import com.handtransaction.service.GoodsService;
import com.handtransaction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Jaywee
 * @theme
 * @date 2021/4/6 19:31
 * @note
 */
@Controller
public class PublishController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    UserService userService;

    /**
     * 获取用户发布的商品
     * @param goods
     * @param modelAndView
     * @param model
     * @param uid
     * @return
     */
    @RequestMapping(value = "showMyGoods", method = RequestMethod.GET)
    public ModelAndView showMyGoods(Goods goods, ModelAndView modelAndView, Model model
            , @RequestParam(value = "uid", required = true) int uid) {
        List<Goods> list = goodsService.queryGoodsByGuid(uid);
        model.addAttribute("goodsPublish", list);
        modelAndView.setViewName("user/userPublish");
        return modelAndView;
    }

    /**
     * 处理用户删除商品（下架）
     * @param modelAndView
     * @param gid
     * @return
     */
    @RequestMapping(value = "deleteGoods", method = RequestMethod.GET)
    public ModelAndView deleteOrder(ModelAndView modelAndView,
                                    @RequestParam(value = "gid") Integer gid) {
        goodsService.DeleteGoodsByGid(gid);
        modelAndView.setViewName("common/succ");
        return modelAndView;
    }

    /**
     * 处理用户编辑商品
     * @param modelAndView
     * @param gid
     * @param uid
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "goodsDetail",method = RequestMethod.GET)
    public ModelAndView goodsDetail(
            ModelAndView modelAndView,
            @RequestParam(value = "gid",required = true)int gid
            , @RequestParam(value = "uid",required = false)Integer uid,
            HttpServletRequest request, HttpServletResponse response){
        Goods goods1 = goodsService.QueryGoodBygid(gid);
        modelAndView.addObject("goods1",goods1);
        request.getSession().setAttribute("good1",goods1);
        User user1 = userService.QueryUserByuid(uid);
        modelAndView.addObject("user1",user1);
        modelAndView.setViewName("goods/goodsUpdate");
        return modelAndView;
    }
}
